﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;


using com.vs.DataAccess;
using com.vs.General;

namespace com.vs.DataAccess.DO
{
    public partial class AuditLogDO
    {
        #region "Get Model"
        /// <summary>
        /// 将符合数据库主键的持久对象提取到模型对象中，如数据库符合条件的持久对象不存在，则返回空对象
        /// </summary>
        /// <remarks></remarks>
        public static AuditLogDO GetModelByPK(Session sess, int iIntID)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            string stSQL = SelectSequel + " WHERE INTERNAL_ID = @INTERNAL_ID";
            try
            {
                helper = new DataHelper(sess);
                dtModel = helper.GetTable(CommandType.Text, stSQL, new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_ID", iIntID)
                });
                if (!DataManager.IsEmpty(dtModel))
                {
                    return GetModel(dtModel.Rows[0]);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        #endregion

        #region "DataBase Operation"
        /// <summary>
        /// 在数据库里创建该数据访问对象的持久化数据，同时自增长属性INTERNAL_APPOINT_NUM也会同时得到更新,并提供数据库事务支持
        /// </summary>
        /// <remarks></remarks>
        public string Insert(Session sess)
        {
            DataHelper helper = null;
            IDataParameter[] paras = null;
            string stSQL = "LOG_IAuditLog";
            try
            {
                helper = new DataHelper(sess);
                paras = ValueParas(this);
                return "" + helper.GetInt(CommandType.StoredProcedure, stSQL, paras);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public string Delete(Session sess)
        {
            DataHelper helper = null;
            string stSQL = "DELETE FROM AUDIT_LOG WHERE INTERNAL_ID = @INTERNAL_ID";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.Text, stSQL, new IDataParameter[]{
                        DataHelper.BuildParameter(sess, "@INTERNAL_ID", this.INTERNAL_ID)});
                return null;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        /// <summary>
        /// 将该数据访问对象的变化同步到数据库中，如果该数据访问对象在数据库中的持久数据不存在，同步不会发生，同时也不会抛出例外
        /// </summary>
        /// <remarks></remarks>
        public void Update(Session sess)
        {
            DataHelper helper = null;
            string stSQL = "LOG_UAuditLog";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, stSQL, ValueParas(this));
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public string Update(Session sess, Dictionary<string, object> dcData)
        {
            DataHelper helper = null;
            StringBuilder sbSQL = null;
            string stSQL = null;
            int i = 0;
            IDataParameter[] paras = null;

            try
            {
                if (dcData.Count < 1)
                    return null;
                helper = new DataHelper(sess);
                sbSQL = new StringBuilder(256)
                            .Append(" UPDATE AUDIT_LOG SET ");
                paras = new IDataParameter[dcData.Count + 1];
                foreach (KeyValuePair<string, object> kvp in dcData)
                {
                    sbSQL.Append(kvp.Key).Append(" = @Value").Append(i).Append(",");
                    paras[i] = new SqlParameter("@Value" + i, kvp.Value);
                    i++;
                }

                stSQL = sbSQL.ToString();
                stSQL = stSQL.Substring(0, stSQL.Length - 1);

                stSQL = stSQL + " WHERE INTERNAL_ID = @INTERNAL_ID";
                paras[i] = new SqlParameter("@INTERNAL_ID", this.INTERNAL_ID);

                helper.Update(CommandType.Text, stSQL, paras);

                return null;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }
        #endregion

        #region "Data Load"
        /// <summary>
        /// 装载对象RECEIPHEADER的所有持久性对象到集合，如果数据量大，调用此方法会造成性能上的问题，请谨慎使用
        /// </summary>
        /// <remarks></remarks>
        public static List<AuditLogDO> GetAll(Session sess)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            string stSQL = SelectSequel;
            try
            {
                helper = new DataHelper(sess);
                dtModel = helper.GetTable(CommandType.Text, stSQL);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        /// <summary>
        /// 装载指定列的值等于指定值的的所有持久性RECEIPHEADER对象到集合
        /// </summary>
        /// <remarks></remarks>
        public static List<AuditLogDO> GetListByColumn(Session sess, string columnName, Object value)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            IDataParameter[] paras = null;
            string stSQL = null;
            try
            {
                helper = new DataHelper(sess);
                stSQL = AuditLogDO.SelectSequel + " Where [" + columnName + "] =@Value ";
                paras = new IDataParameter[]{
                    //DataHelper.BuildParameter(sess, "@Value", value)
                    new SqlParameter( "@Value", value)
                };
                dtModel = helper.GetTable(CommandType.Text, stSQL, paras);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public static DataTable GetTableByColumn(Session sess, string columnName, Object value)
        {
            DataHelper helper = null;
            IDataParameter[] paras = null;
            string stSQL = null;
            try
            {
                helper = new DataHelper(sess);
                stSQL = AuditLogDO.SelectSequel + " Where [" + columnName + "] =@Value ";
                paras = new IDataParameter[]{
                    //DataHelper.BuildParameter(sess, "@Value", value)
                    new SqlParameter( "@Value", value)
                };
                return helper.GetTable(CommandType.Text, stSQL, paras);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        /// <summary>
        /// 从DataView装载持久性RECEIPHEADER对象到集合
        /// </summary>
        /// <remarks></remarks>
        private static List<AuditLogDO> LoadListFromDataView(DataView dv)
        {
            List<AuditLogDO> list = new List<AuditLogDO>();
            for (int index = 0; index <= dv.Count - 1; index++)
            {
                list.Add(GetModel(dv[index].Row));
            }
            return list;
        }

        /// <summary>
        /// loading data value  to property from Datarow
        /// </summary>
        /// <remarks></remarks>
        private static AuditLogDO GetModel(DataRow row)
        {
            AuditLogDO apptDO = new AuditLogDO();
            if (!row["INTERNAL_ID"].Equals(DBNull.Value)) apptDO.INTERNAL_ID = Convert.ToInt32(row["INTERNAL_ID"]);
            if (!row["WAREHOUSE"].Equals(DBNull.Value)) apptDO.WAREHOUSE = Convert.ToString(row["WAREHOUSE"]);
            if (!row["MESSAGE"].Equals(DBNull.Value)) apptDO.MESSAGE = Convert.ToString(row["MESSAGE"]);
            if (!row["USER_STAMP"].Equals(DBNull.Value)) apptDO.USER_STAMP = Convert.ToString(row["USER_STAMP"]);
            if (!row["DATE_TIME_STAMP"].Equals(DBNull.Value)) apptDO.DATE_TIME_STAMP = Convert.ToDateTime(row["DATE_TIME_STAMP"]);
            if (!row["VALUE"].Equals(DBNull.Value)) apptDO.VALUE = Convert.ToString(row["VALUE"]);

            return apptDO;
        }
        #endregion

        #region "Other function"
        /// <summary>
        /// 该数据访问对象从数据库中提取数据的Sql语句 
        /// </summary>
        /// <remarks></remarks>
        public static string SelectSequel
        {
            get
            {
                return "Select	*  From [AUDIT_LOG] ";
            }
        }

        /// <summary>
        /// 该数据访问对象的属性值装载到数据库更新参数数组
        /// </summary>
        /// <remarks></remarks>
        private static IDataParameter[] ValueParas(AuditLogDO apptDO)
        {
            SqlParameter[] paras = new SqlParameter[6];
            paras[0] = new SqlParameter("@INTERNAL_ID", apptDO.INTERNAL_ID);
            paras[1] = new SqlParameter("@WAREHOUSE", apptDO.WAREHOUSE == String.Empty ? DBNull.Value : (object)apptDO.WAREHOUSE);
            paras[2] = new SqlParameter("@MESSAGE", apptDO.MESSAGE == String.Empty ? DBNull.Value : (object)apptDO.MESSAGE);
            paras[3] = new SqlParameter("@USER_STAMP", apptDO.USER_STAMP == String.Empty ? DBNull.Value : (object)apptDO.USER_STAMP);
            paras[4] = new SqlParameter("@DATE_TIME_STAMP", apptDO.DATE_TIME_STAMP == DateTime.MinValue ? DBNull.Value : (object)apptDO.DATE_TIME_STAMP);
            paras[5] = new SqlParameter("@VALUE", apptDO.VALUE == String.Empty ? DBNull.Value : (object)apptDO.VALUE);

            paras[0].DbType = DbType.Int32;
            paras[1].DbType = DbType.String;
            paras[2].DbType = DbType.String;
            paras[3].DbType = DbType.String;
            paras[4].DbType = DbType.DateTime;
            paras[5].DbType = DbType.String;


            return paras;
        }

        private static IDataParameter[] UpdateWherePara(AuditLogDO recHdrDO)
        {
            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@INTERNAL_ID", recHdrDO.INTERNAL_ID);
            return paras;
        }

        #endregion

        public static List<AuditLogDO> GetListByColumns(Session sess, List<KeyValuePair<string, KeyValuePair<string, object>>> dcParam, string stOrderBy)
        {
            DataTable dtModel = null;

            try
            {
                dtModel = GetTableByColumns(sess, dcParam, stOrderBy);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
            }
        }

        public static DataTable GetTableByColumns(Session sess, List<KeyValuePair<string, KeyValuePair<string, object>>> dcParam, string stOrderBy)
        {
            DataHelper helper = null;
            StringBuilder sbSQL = null;
            int i = 0;
            IDataParameter[] pars = null;

            try
            {
                helper = new DataHelper(sess);
                sbSQL = new StringBuilder(SelectSequel)
                    .Append(" WHERE 1=1 ");

                pars = new IDataParameter[dcParam.Count];
                foreach (KeyValuePair<string, KeyValuePair<string, object>> de in dcParam)
                {
                    sbSQL.Append(" AND ").Append(de.Key).Append(de.Value.Key).Append("@Value").Append(i);
                    pars[i] = new SqlParameter("@Value" + i, de.Value.Value);
                    i++;
                }

                if (!StringManager.IsEmpty(stOrderBy))
                {
                    sbSQL.Append(stOrderBy);
                }
                return helper.GetTable(CommandType.Text, sbSQL.ToString(), pars);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }
    }
}
